#!/bin/ash

timeout=5
ACTION=$1
DEVICES="/dev/ttyUSB1 /dev/ttyUSB2"
DEVICE=/dev/ttyUSB1
info_file=/tmp/ttyUSBx.info
log_file=/tmp/SLM630B.log
usb_num=`cat /etc/config/network | grep 4g -A 2 | grep ifname | awk -F"\'" '{print $2}'`

safe_at_cmd()
{
	if [ -c $DEVICE ]; then
		#echo "$1" > /dev/console
		echo $1 >	$DEVICE
	else
		echo "no dev" >> ${log_file}
	fi
}

get_avail_device()
{
	for i in $DEVICES;do
	DEVICE=$i
	echo "cat $DEVICE > $info_file &" >> ${log_file}
	#kill_process $DEVICE
	check_sim
	if [ $? -eq 0 ]; then
	 echo "find availabe device -> [$DEVICE]" >> ${log_file}
	 return
	fi
	done
	
	echo "can not find availabe device" >> ${log_file}
}

interface_restart()
{
	ifconfig usb0 down
	ifconfig usb0 up
}

m_rm()
{
	file=$1
	
	Bytes=`ls -l $file | awk '{printf $5}'`
	
	if [ "$Bytes" -gt "1000" ]; then
		rm $file
		echo "too lagre ! rm it" > $log_file
	fi
}

start()
{
	ifconfig usb0 up
	#interface_restart
	ps | grep usb0
	while true
	do
	
	#check_device
	if [ "1" = "1" ]; then
	        check_sim
	        if [ $? -eq 0 ]; then
	                connect_internet
	                if [ $? -eq 0 ]; then
	                        while true
	                        do
	                                check_usb
	                                if [ $? -eq 0 ];then
	                                        check_usb_ipaddr
	                                        if [ $? -eq 0 ]; then
	                                                sleep $timeout
	                                        				m_rm $log_file
	                                                continue
	                                        else
	                                        				#interface_restart
	                                                break
	                                        fi
	                                fi
	                                sleep $timeout
	                        done
	                fi
	        fi
	fi
	echo "no signal must here!" >> $log_file
	get_avail_device
	sleep $timeout
	#rm $info_file
	m_rm $log_file
	
	
	done
}

stop()
{
	ifconfig usb0 down
	get_avail_device
	echo "stop device=$DEVICE" > /dev/console
	safe_at_cmd AT^NDISDUP=1,0
	#echo AT^NDISDUP=1,0 > $DEVICE
	m_rm $log_file
}

check_device()
{
		kver=`uname -r`
		
    if [ ! -e "$DEVICE" ]; then
        a=`cat /sys/bus/usb/devices/1-1.2/idVendor`
        b=`cat /sys/bus/usb/devices/1-1.2/idProduct`
        rmmod option
        rmmod usb_wwan
        rmmod usbserial
        insmod /lib/modules/$kver/usbserial.ko vendor=0x$a product=0x$b
    if [ ! -e "$DEVICE" ]; then
        echo "insmod modules failed" >> $log_file
        return 1
    fi
        return 0
    fi
}

check_sim()
{
    echo "checking sim..." >> $log_file
    echo "echo at+cpin? > $DEVICE" >> $log_file
    if [ ! -c $DEVICE ]; then
    	return
    fi
    safe_at_cmd at+cpin?
    #echo at+cpin? > $DEVICE
    cat $DEVICE > $info_file &
    sleep 1
    kill_process $DEVICE
    
    a=`cat $info_file | grep "+CPIN" | awk -F ":" 'END{print $2}'`
    echo "a=[$a]" >> $log_file
    if [ "X$a" == "X READY" ]; then
        echo "SIM OK!" >> $log_file
        return 0
    else
        echo `cat $info_file | grep "ERROR" | awk -F ":" 'END{print $2}'` >> $log_file
        return 1
    fi
}

connect_internet()
{
    echo "conneting internet..." >> $log_file
    if [ ! -c $DEVICE ]; then
    	return
    fi
    safe_at_cmd at+cops?
    #echo at+cops? > $DEVICE
    cat $DEVICE > $info_file &
    sleep 1
    kill_process $DEVICE
    
        b=`cat $info_file | grep "+COPS" |awk -F "," '{print $3}' | awk -F "\"" 'END{print $2}'`
        if [ "X$b" == "XCHN-TELECOM" ]; then
        				#safe_at_cmd AT^NDISDUP=1,1,\"\ctnet\"\,\"\"\,\"\"\,0
                echo AT^NDISDUP=1,1,\"\ctnet\"\,\"\"\,\"\"\,0 > $DEVICE
                sleep 3
                echo "CHN-TELECOM OK" >> $log_file
                return 0
        elif [ "X$b" == "XCHN-UNICOM" ]; then
        				#safe_at_cmd AT^NDISDUP=1,1,\"\3gnet\"\,\"\"\,\"\"\,0
                echo AT^NDISDUP=1,1,\"\3gnet\"\,\"\"\,\"\"\,0 > $DEVICE
                sleep 3
                echo "CHN-UNTCOM OK" >> $log_file
                return 0
        elif [ "X$b" == "XCHINA  MOBILE" ]; then
        				#safe_at_cmd AT^NDISDUP=1,1,\"\cmnet\"\,\"\"\,\"\"\,0
                echo AT^NDISDUP=1,1,\"\cmnet\"\,\"\"\,\"\"\,0 > $DEVICE
                sleep 3
                echo "CHINA MOBILE OK" >> $log_file
                return 0
        else
                echo "NO--SIGIN----!" >> $log_file
                return 1
        fi

}

check_usb()
{
        usb=`ifconfig | grep "$usb_num"`
        if [ -z "$usb" ];then
                echo "$usb_num is now down" >> $log_file
                return 1
        else
                echo "$usb_num is now up" >> $log_file
                return 0
        fi

}

check_usb_ipaddr()
{
        usb_ipaddr=`ifconfig $usb_num | grep "inet addr" `
        if [ -z "$usb_ipaddr" ]; then
                echo "$usb_num no ipaddr" >> $log_file
                return 1
        else
                echo "$usb_ipaddr" >> $log_file
                return 0
        fi
}


kill_process()
{
        process_id=`ps | grep "cat $1" | awk '{print $1}' | sed '$d'`
        for i in $process_id; do
        				echo "kill $i" >> $log_file
                kill $i
        done
}

#kill all cat /dev/ttyUSBx 
for i in $DEVICES;do
	kill_process $i 
done


#find available device to send at command

get_avail_device

if [ "$ACTION" = "start" ]; then
	start
elif [ "$ACTION" = "stop" ]; then
	stop
else
	echo "SLM630B.sh not support this action ---> action=[$ACTION]" > /dev/console
fi

